package com.zhang.family_doctor.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * 拦截器
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;


    /**
     * 解决跨域请求
     * @param registry
     */

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }


    /**
     * 异步请求配置
     * @param configurer
     */

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }


    /**
     * 配置拦截器、拦截路径
     * 每次请求到拦截的路径，就会去执行拦截器中的方法
     * @param
     */

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除拦截，除了注册登录(此时还没token)，其他都拦截
        excludePath.add("/resident/sendMsg"); //居民注册发送验证码
        excludePath.add("/resident/register"); //居民注册
        excludePath.add("/resident/login"); //居民账号登录
        excludePath.add("/resident/noLogin"); //居民账号登录
        excludePath.add("/resident/forgotPassword"); //忘记密码
        excludePath.add("/doctor/login"); //后台系统登录
        excludePath.add("/doctor/logins"); //后台系统登录
        excludePath.add("/doctor/loginDoctor"); //医生端登录
        excludePath.add("/doc.html/**"); //knife4j  Swagger
        excludePath.add("/swagger-resources/**"); //swagger
        excludePath.add("/swagger-ui.html/**");   //swagger
        excludePath.add("/webjars/**");           //swagger
        excludePath.add("/v2/**");                //swagger
        //excludePath.add("/signUp/paymentSucceeded");//给第三方调用的支付成功接口

        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);

    }
}
